package com.imay.live.fragment;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.fragment.BaseFragment;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.loader.DataLoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

/**
 * Created by jeaman on 2017/9/26.
 */

public abstract class BaseLoadDataFragment extends BaseFragment implements ProtoCallBack, DataLoader.OnRefreshListener {
    private DataLoader mDataLoader;

    private boolean isFirstVisible;
    protected String mRequestCommand;
    protected String mRefreshCommand;
    protected String mLoadMordCommand;

    @CallSuper
    @Override
    protected void initView(Bundle savedInstanceState) {
        mDataLoader = new DataLoader(this);
        mDataLoader.initRecyclerView(getRecyclerView(), getRecyclerViewLayoutManager(), getAdapter(), isEnableLoadMore());
        mDataLoader.initRefreshLayout(getRefreshLayout());

        if (isAutoLoadData() && !isLazyLoadData()) {
            onRequestData();
        }
    }

    //创建RecyclerView
    protected abstract RecyclerView getRecyclerView();

    //创建RefreshLayout，如果返回null表示不需要下拉刷新
    protected abstract TwinklingRefreshLayout getRefreshLayout();

    //创建adapter
    protected abstract BaseQuickAdapter getAdapter();

    //请求数据
    protected abstract void onRequestData();

    //刷新数据
    protected abstract String onRefreshData();

    //加载更多数据
    protected abstract String onLoadMoreData();

    //返回数据
    protected abstract List processRequestData(String data);

    //是否自动加载数据
    protected boolean isAutoLoadData() {
        return true;
    }

    //是否懒加载数据
    protected boolean isLazyLoadData() {
        return true;
    }

    //是否使用加载更多
    protected boolean isEnableLoadMore() {
        return true;
    }

    //默认线性布局
    protected RecyclerView.LayoutManager getRecyclerViewLayoutManager() {
        return new WrapContentLinearLayoutManager(getContext());
    }

    protected int getDataSize() {
        return mDataLoader.getDataSize();
    }

    @Override
    public final void onLoadMoreRequested() {
        mLoadMordCommand = onLoadMoreData();
    }

    @Override
    public void onRefreshRequested() {
        mRefreshCommand = onRefreshData();
    }

    @Override
    public void onRetryRequested() {
        onRequestData();
    }

    @Override
    public final void onRequestSuccess(String command, String data) {
        if (!isAdded()) {
            return;
        }
        if (command.equals(mRefreshCommand)) {
            mDataLoader.refreshComplete();
        } else if (command.equals(mLoadMordCommand)) {
            mDataLoader.loadMoreComplete();
        }
        if (command.equals(mRequestCommand)) {
            List dataList = processRequestData(data);
            if (command.equals(mLoadMordCommand)) {  //加载更多
                mDataLoader.addData(dataList);
            } else {    //刷新或者第一次加载数据
                mDataLoader.setNewData(dataList);
            }
        }
    }

    @Override
    public final void onRequestError(String command, int code, String message) {
        if (!isAdded()) {
            return;
        }
        if (command.equals(mRefreshCommand)) {
            mDataLoader.refreshComplete();
        } else if (command.equals(mLoadMordCommand)) {
            mDataLoader.loadMoreComplete();
        }
        if (command.equals(mRequestCommand)) {
            //显示错误信息
            if (command.equals(mRefreshCommand)) {
                //下拉刷新，toast
                ErrorResult.toastError(getContext(), code, message);
            } else if (command.equals(mLoadMordCommand)) {
                //加载更多，显示加载失败
                mDataLoader.loadMoreFail();
            } else {
                //第一次加载数据，显示错误页面
                mDataLoader.showErrorView();
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && !isFirstVisible) {
            isFirstVisible = true;
            if (isAutoLoadData() && isLazyLoadData()) {
                //数据懒加载
                onRequestData();
            }
        }
    }

    @Override
    public void onDestroy() {
        //通过hashCode作为tag
        ProtoRequestHelper.cancelRequest(this.hashCode());
        super.onDestroy();
    }
}

